<template>
    <div class="scoring-standard">
        <el-card>
            <!-- 搜索栏 -->
            <el-form :inline="true" class="search-form">
                <!-- <el-form-item label="用户名称">
                    <el-input
                        v-model="searchParams.userName"
                        placeholder="请输入用户名称"
                    ></el-input>
                </el-form-item> -->
                <el-form-item label="成绩等级">
                    <el-select
                        v-model="searchParams.grade"
                        placeholder="请选择成绩等级"
                    >
                        <el-option label="优秀" value="优秀"></el-option>
                        <el-option label="良好" value="良好"></el-option>
                        <el-option label="及格" value="及格"></el-option>
                        <el-option label="不及格" value="不及格"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchStandards"
                        >搜索</el-button
                    >
                </el-form-item>
            </el-form>



            <!-- 评分标准列表 -->
            <el-table
                :data="standardList"
                style="width: 100%"
                class="standard-table"
                v-loading="loading"
            >
                <el-table-column
                    prop="userName"
                    label="用户名称"
                ></el-table-column>
                <el-table-column
                    prop="score1"
                    label="立定跳远分数"
                ></el-table-column>
                <el-table-column
                    prop="score2"
                    label="耐力跑分数"
                ></el-table-column>
                <el-table-column
                    prop="score3"
                    label="引体向上/仰卧起坐分数"
                ></el-table-column>
                <el-table-column
                    prop="score4"
                    label="50米跑分数"
                ></el-table-column>
                <el-table-column
                    prop="score5"
                    label="坐位体前屈分数"
                ></el-table-column>
                <el-table-column
                    prop="score6"
                    label="体重指数"
                ></el-table-column>
                <el-table-column
                    prop="score7"
                    label="肺活量"
                ></el-table-column>
                <el-table-column
                    prop="score"
                    label="总分"
                ></el-table-column>
                <el-table-column
                    prop="grade"
                    label="成绩等级"
                ></el-table-column>
   

            </el-table>

            <!-- 分页 -->
            <el-pagination
                class="pagination"
                :current-page="currentPage"
                :page-size="pageSize"
                :total="total"
                @current-change="handlePageChange"
            ></el-pagination>
        </el-card>

        <!-- 添加/编辑评分标准对话框 -->
        <el-dialog :title="dialogTitle" v-model="dialogVisible">
            <el-form :model="standardForm">
                <el-select
                    v-model="standardForm.projectId"
                    v-if="projectList.length > 0"
                >
                    <el-option
                        v-for="project in projectList"
                        :key="project.projectId"
                        :label="project.projectName"
                        :value="project.projectId"
                    ></el-option>
                </el-select>
                <el-form-item label="分数">
                    <el-input v-model="standardForm.minScore"></el-input>
                </el-form-item>
  
                <el-form-item label="成绩等级">
                    <el-select v-model="standardForm.grade">
                        <el-option label="优秀" value="优秀"></el-option>
                        <el-option label="良好" value="良好"></el-option>
                        <el-option label="及格" value="及格"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="saveStandard">保存</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import {
    getScoringStandardList,
    addScoringStandard,
    updateScoringStandard,
    deleteScoringStandard
} from '@/api/scoringStandard'
import { getFitnessProjectList } from '@/api/fitnessProject'

export default {
    data() {
        return {
            searchParams: { userName: '', grade: '' },
            standardList: [],
            projectList: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            loading: false,
            dialogVisible: false,
            dialogTitle: '添加评分标准',
            standardForm: {
                project_id: '',
                minScore: '',
                maxScore: '',
                grade: '',
                description: ''
            }
        }
    },
    created() {
        this.fetchProjects()
        this.fetchStandards()
    },
    methods: {
        async fetchStandards() {
            this.loading = true
            try {
                const response = await getScoringStandardList({
                    page: this.currentPage,
                    size: this.pageSize,
                    ...this.searchParams
                })
                this.standardList = response.data.records
                this.standardList.map((project) => {
                    if (project.score1 == 0) project.score1 = "未上传或为0"
                    if (project.score2 == 0) project.score2 = "未上传或为0"
                    if (project.score3 == 0) project.score3 = "未上传或为0"
                    if (project.score4 == 0) project.score4 = "未上传或为0"
                    if (project.score5 == 0) project.score5 = "未上传或为0"
                    if (project.score6 == 0) project.score6 = "未上传或为0"
                    if (project.score7 == 0) project.score7 = "未上传或为0"
                })
                this.total = response.total
            } catch (error) {
                this.$message.error('获取评分标准列表失败')
            } finally {
                this.loading = false
            }
        },
        async fetchProjects() {
            try {
                const response = await getFitnessProjectList()
                // 确保数据结构正确
                this.projectList = response.data.records.map((item) => ({
                    projectId: item.projectId, // 确保字段名一致
                    projectName: item.projectName // 确保字段名一致
                }))
    
            } catch (error) {
                this.$message.error('获取项目列表失败')
            }
        },
        searchStandards() {
            this.currentPage = 1
            this.fetchStandards()
        },
        openAddStandardDialog() {
            this.dialogTitle = '添加评分标准'
            this.standardForm = {}
            this.dialogVisible = true
        },
        editStandard(standard) {
            this.dialogTitle = '编辑评分标准'
            this.standardForm = { ...standard }
            this.dialogVisible = true
        },
        async saveStandard() {
            try {
                if (this.standardForm.standard_id) {
                    await updateScoringStandard(this.standardForm)
                } else {
                    await addScoringStandard(this.standardForm)
                }
                this.$message.success('保存成功')
                this.dialogVisible = false
                this.fetchStandards()
            } catch (error) {
                this.$message.error('保存失败')
            }
        },
        async deleteStandard(standardId) {
            try {
                await deleteScoringStandard(standardId)
                this.$message.success('删除成功')
                this.fetchStandards()
            } catch (error) {
                this.$message.error('删除失败')
            }
        },
        handlePageChange(page) {
            this.currentPage = page
            this.fetchStandards()
        }
    }
}
</script>
<style scoped>
.scoring-standard {
    padding: 20px;
}
.search-form {
    margin-bottom: 20px;
}
.standard-table {
    margin-top: 20px;
}
.pagination {
    margin-top: 20px;
    text-align: right;
}
</style>


